<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/30 0030
 * Time: 16:28
 */

namespace app\admin\model;


use think\Model;

class News extends Model
{


    /**
     * 获取单条
     * @param $id
     * @return array|null|\PDOStatement|string|Model
     */
    public function _findOne($id){
        return $this->where(['id' => $id])->find();
    }


    /**
     * 列表
     * @param $condition
     * @param int $page_size
     * @param string $order_by
     * @return \think\Paginator
     */
    public function _list($condition, $page_size = PAGE_SIZE_ADMIN, $order_by = 'a.is_set_top desc,a.update_at desc'){
        $query['query'] = request()->param();
        $filed =
            '
            a.id,
            a.sort,
            a.title,
            a.logo,
            a.n_t_id,
            a.is_set_top,
            a.create_at,
            a.update_at,
            b.t_name as n_t_id_str
            ';
        $result = $this->alias('a')
            ->field($filed)
            ->leftJoin(['news_types' => 'b'], 'a.n_t_id = b.id')
            ->where($condition)
            ->order($order_by)
            ->paginate($page_size, false, $query);
        return $result;
    }


    /**
     * 删除
     * @param $id
     * @return bool
     */
    public function _remove($id){
        $result = $this->_findOne($id);
        if(!$result){
            $this->error = '没有数据';
            return false;
        }
        $logo_r = $result->logo;
        if(!$result->delete()){
            $this->error = '操作失败';
            return false;
        }
        \QiNiuHelper::del_file($logo_r);
        return true;
    }


    /**
     * 详情
     * @param $id
     * @return array|bool
     */
    public function _detail($id){
        $result = $this->_findOne($id);
        if(!$result){
            $this->error = '没有数据';
            return false;
        }
        $result = $result->toArray();
        $result['logo'] = getImgHttpName($result['logo']);
        $news_type = (new NewsTypes())->_findOne($result['n_t_id']);
        $result['n_t_id_str'] = $news_type ? $news_type['t_name'] : '';
        return $result;
    }


    /**
     * 添加
     * @param $admin_id
     * @param $n_t_id
     * @param $title
     * @param $desc
     * @param $content
     * @param $sort
     * @param $file)
     * @return bool
     */
    public function _create($admin_id, $n_t_id, $title, $desc, $content, $sort, $file){
        $file_name = getFileUploadName($file['name']);
        $tmp_name = $file['tmp_name'];
        $file_path = \QiNiuHelper::server_upload($file_name, $tmp_name);
        if(!$file_path){
            $this->error = '上传图片失败';
            return false;
        }
        $this->admin_id = $admin_id;
        $this->n_t_id = $n_t_id;
        $this->title = $title;
        $this->desc = $desc;
        $this->logo = $file_path;
        $this->sort = $sort;
        $this->content = $content;
        if(!$this->save()){
            $this->error = '添加失败';
            \QiNiuHelper::del_file($file_path);
            return false;
        }
        return true;
    }


    /**
     * 修改
     * @param $admin_id
     * @param $id
     * @param $n_t_id
     * @param $title
     * @param $desc
     * @param $content
     * @param $sort
     * @param bool $file
     * @return bool
     */
    public function _modify($admin_id, $id, $n_t_id, $title, $desc, $content, $sort, $file = false){
        $result = $this->_findOne($id);
        if(!$result){
            $this->error = '没有数据';
            return false;
        }
        $file_path = false;
        $logo_r = $result->logo;
        if($file){
            $file_name = getFileUploadName($file['name']);
            $tmp_name = $file['tmp_name'];
            $file_path = \QiNiuHelper::server_upload($file_name, $tmp_name);
            if(!$file_path){
                $this->error = '上传图片失败';
                return false;
            }
            $result->logo = $file_path;
        }
        $result->admin_id = $admin_id;
        if($n_t_id) $result->n_t_id = $n_t_id;
        if($title) $result->title = $title;
        if($title) $result->desc = $desc;
        if($title) $result->content = $content;
        if($title) $result->sort = $sort;
        if($result->save() === false){
            $this->error = '修改失败';
            if($file_path){
                \QiNiuHelper::del_file($file_path);
            }
            return false;
        }
        if($file_path){
            \QiNiuHelper::del_file($logo_r);
        }
        return true;
    }



}